package com.nunbey.lingxi.module.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.nunbey.lingxi.R;
import com.nunbey.lingxi.module.mode.goods.Good;

import java.util.ArrayList;

/**
 * @description 首页商品列表
 */
public class GoodRecycleView {
    /**
     * @description 组件控制者
     */
    private static class GoodHolder extends RecyclerView.ViewHolder{
        private final ImageView image;
        private final TextView name;
        private final TextView price;
        public GoodHolder(@NonNull View itemView) {
            super(itemView);
            this.image = itemView.findViewById(R.id.list_good_image);
            this.name = itemView.findViewById(R.id.list_good_name);
            this.price = itemView.findViewById(R.id.list_good_price);
        }
    }

    public static class GoodRecycleViewAdapter extends RecyclerView.Adapter{

        ArrayList<Good> goods;
        Context context;

        public  GoodRecycleViewAdapter(ArrayList<Good> goods, Context context){
            this.goods=goods;
            this.context=context;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_good,parent,false);
            GoodHolder goodHolder = new GoodHolder(view);

            return goodHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            Good goodItem = goods.get(position);
            GoodHolder goodHolder = (GoodHolder) holder;
            Glide.with(context).load(goodItem.getPictureUrl()).into(goodHolder.image);
            goodHolder.name.setText(goodItem.getGoodName());
            goodHolder.price.setText(Double.toString(goodItem.getGoodPrice())+"元");
        }

        @Override
        public int getItemCount() {
            return goods.size();
        }
    }
}
